<?php
/**
 * Created by PhpStorm.
 * User: wolf
 * Date: 2018/8/24
 * Time: 11:01
 */

namespace App\Http\Controllers\MiniApp;


use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\BannerItem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BannerController extends Controller
{
    // 首页头图
    public function index(){
        try{
//            $rules = [
//                'id'=>'integer'
//            ];
//            $message = [
//                'id.*'=>'请求参数有误'
//            ];
//            $validator = Validator::make($request->all(), $rules,$message);
//            if ($validator->fails()) {
//                return show(201,$validator->errors()->first());
//            }
//            $id = $request->id;
            $head_id = 1;   // 头部轮播图
            $mid_id = 2;    // 中部导航图
//            $banner_head =Banner::with(['items'=>function($query){
//                $query->limit(3);
//            }])->where('id',$head_id)->first();
//
//            $banner_mid =Banner::with(['items'=>function($query){
//                $query->limit(3);
//            }])->where('id',$mid_id)->first();
            $banner_head = BannerItem::where('banner_id',$head_id)->where('img_type',2)->limit(3)->get();
            $banner_mid = BannerItem::where('banner_id',$mid_id)->where('img_type',2)->limit(2)->get();
            return show(200,'OK',['banner'=>$banner_head,'middle'=>$banner_mid]);
        }catch (\Exception $e){
            return show(201,'FAIL');
        }


    }
}